package com.example.springbatch_itemreader_json;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.metrics.BatchMetrics;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.Date;

@Component("myJobExecutionListener")
@Slf4j
public class MyJobExecutionListener implements JobExecutionListener {
    private Date beginTime;

    private Date endTime;


    @Override
    public void beforeJob(JobExecution jobExecution) {
        beginTime = new Date();
        log.info("开始时间:{}", beginTime);
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        endTime = new Date();
        log.info("结束时间:{}", endTime);

        Duration stepExecutionDuration = BatchMetrics.calculateDuration(beginTime, endTime);

        log.info("总执行时间:{}", BatchMetrics.formatDuration(stepExecutionDuration));
    }
}
